
(function(jQuery)
{   
    
    jQuery.fn.blackCalculator = function(options) {
        var settings = jQuery.extend({}, jQuery.fn.blackCalculator.defaults, options);
		
		if (settings.type == 'advanced') {
			var whiteList = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '+', '*', '/', '(', ')', '^', '%', '.'];
		} else {
			var whiteList = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '+', '*', '/', '.'];
		}
		
        
        var form = '<form method="post" action="jQueryavascript:void(0)" id="blackCalculatorForm"><fieldset>';
		form += '<label for="blackCalculator">' + settings.language.value + '</label>';
        form += '<input type="text" name="blackCalculator" id="blackCalculator"/>';
		form += '<div id="text_retencion" style="display:none; float:left;">Tipo Retenci&oacute;n:&nbsp;</div>';
		form += '<select name="tipo_sueldo" id="tipo_sueldo" style="display:none; float:left;">';
		form += '<option value="n">Seleccionar</option>';
		form += '<option value="m">Mensual</option>';
		form += '<option value="q">Quincenal</option>';
		form += '</select>';
		form += '<div class="isss"></div>';
		form += '<div class="afp"></div>';
		form += '<div class="isr_mensual"></div>';
		form += '<div class="isr_quincenal"></div>';
                form += '<div class="total_retencion"></div>';
		form += '</fieldset></form><br>';

        jQuery(this).addClass('blackCalculator');

        jQuery(this).prepend(form);
       
		// line 1
		var lines = '<a href="jQueryavascript:void(0)" title="Borrar" style="width:20px; margin:0;" rel="<">&lArr;</a>';
		//' + settings.language.backspace+ '
		lines += '<a href="jQueryavascript:void(0)" title="Retenciones" rel="ret" style="width:88px;">Retenciones</a>';
		if (settings.type == 'advanced') {
			lines += '<a href="jQueryavascript:void(0)" title="Limpiar Todo" rel="c">C</a>';
			lines += '<a href="jQueryavascript:void(0)" title="(" rel="(">(</a>';
			lines += '<a href="jQueryavascript:void(0)" title=")" rel=")">)</a>';
		} else {
			lines += '<a href="jQueryavascript:void(0)" title="Limpiar Todo" rel="c" style="width:25px;">C</a>';
		}
		//' + settings.language.clear + '
		
		// line 2
		lines += '<a href="jQueryavascript:void(0)" title="7" style="margin:0;" rel="7">7</a>';
        lines += '<a href="jQueryavascript:void(0)" title="8" rel="8">8</a>';
        lines += '<a href="jQueryavascript:void(0)" title="9" rel="9">9</a>';
		
		if (settings.type == 'advanced') {
			lines += '<a href="jQueryavascript:void(0)" title="/" rel="/">/</a>';
			lines += '<a href="jQueryavascript:void(0)" title="%" rel="%">%</a>';
		} else {
			lines += '<a href="jQueryavascript:void(0)" title="/" rel="/" style="width:57px;">/</a>';
		}
		
		// line 3
		lines += '<a href="jQueryavascript:void(0)" title="4" style="margin:0;" rel="4">4</a>';
        lines += '<a href="jQueryavascript:void(0)" title="5" rel="5">5</a>';
        lines += '<a href="jQueryavascript:void(0)" title="6" rel="6">6</a>';
		
		if (settings.type == 'advanced') {
			lines += '<a href="jQueryavascript:void(0)" title="*" rel="*">*</a>';
			lines += '<a href="jQueryavascript:void(0)" title="yx" rel="yx">y<sup>x</sup></a>';
		} else {
			lines += '<a href="jQueryavascript:void(0)" title="X" rel="*" style="width:57px;">X</a>';
		}
		
		// line 4
		lines += '<a href="jQueryavascript:void(0)" title="1" style="margin:0;" rel="1">1</a>';
        lines += '<a href="jQueryavascript:void(0)" title="2" rel="2">2</a>';
        lines += '<a href="jQueryavascript:void(0)" title="3" rel="3">3</a>';
		
		if (settings.type == 'advanced') {
			lines += '<a href="jQueryavascript:void(0)" title="-" rel="-">-</a>';
		} else {
			lines += '<a href="jQueryavascript:void(0)" title="-" rel="-" style="width:57px;"><div style="position:relative; top:10px; left:24px; background:#2086B3; height:1.5px; width:9px;"></div></a>';
		}
		
        lines += '<div class="clear"></div>';
		
		// line 5
		if (settings.type == 'advanced') {
			lines += '<a href="jQueryavascript:void(0)" title="0" style="width:58px; margin:0;" rel="0">0</a>';
		} else {
			lines += '<a href="jQueryavascript:void(0)" title="0" rel="0" style="margin:0;">0</a>';
		}
		
		
        lines += '<a href="jQueryavascript:void(0)" title="." rel=".">.</a>';
		
		if (settings.type == 'simple') {
			lines += '<a href="jQueryavascript:void(0)" title="=" rel="=">=</a>';
		}
		
		if (settings.type == 'advanced') {
			lines += '<a href="jQueryavascript:void(0)" title="+" rel="+">+</a>';
		} else {
			lines += '<a href="jQueryavascript:void(0)" title="+" rel="+" style="width:57px;">+</a>';
		}
		
		if (settings.type == 'advanced') {
			lines += '<a href="jQueryavascript:void(0)" title="=" rel="=" style="height:40px; padding-top:35px;' +
			'position:absolute; bottom:1px; right:14px;">=</a>';
		}
		
        lines += '<div class="clear"></div>';
		jQuery(this).append(lines);
	
		// calculator actions
	  if (settings.allowKeyboard) {
			jQuery('.blackCalculator').keypress(function(event) {
				var button = String.fromCharCode(event.keyCode);
				var code = event.keyCode;
				var value = jQuery('#blackCalculator').val();
				// if press enter
				if (code == 13) {
					jQuery('.blackCalculator a[rel="="]').trigger('click');
				} else {
					if (!inArray(whiteList, button)) {
						return false;
					}
	
				}
		 
			});
	
		} else {
			jQuery('#blackCalculator').keypress(function(event) {
				return false;
			});
		}
        
		jQuery('.blackCalculator a').click(function() {
			var button = jQuery(this).attr('rel');
			var value = jQuery('#blackCalculator').val();
			
			if (inArray(whiteList, button)) {
				jQuery('#blackCalculator').val(value + button);
			} else {
				if (button == 'c') {
					jQuery('#blackCalculator').val(null);
					jQuery('.isss').html(null);
					jQuery('.afp').html(null);	
					jQuery('.isr_mensual').html(null);
					jQuery('.isr_quincenal').html(null);
                                        jQuery('.total_retencion').html(null);
					jQuery('#tipo_sueldo').css("display","none");
					jQuery('#tipo_sueldo option[value=n]').attr("selected",true);
					jQuery('#text_retencion').css("display","none");
					jQuery('#blackCalculator').focus();
				} else if (button == 'yx') {
					jQuery('#blackCalculator').val(value + '^');
				} else if (button == '<') {
					jQuery('#blackCalculator').val(value.substr(0, value.length - 1));	
				} else if (button == '%') {
					jQuery('#blackCalculator').val(value + '%');
				} else if (button == ',') {
					jQuery('#blackCalculator').val(value + '.');
				} else if (button == '=') {
					try {
				           jQuery('.isss').html(null);
					   jQuery('.afp').html(null);	
					   jQuery('.isr_mensual').html(null);
					   jQuery('.isr_quincenal').html(null);
                                           jQuery('.total_retencion').html(null);
						jQuery('#text_retencion').css("display","none");
						jQuery('#tipo_sueldo option[value=n]').attr("selected",true);
						jQuery('#tipo_sueldo').css("display","none");
                        
						
						// ^ replaced with Math.pow
						var powPattern = /\d+\^\d+/;
						while (strpos(value, '^', 0)) {
							if (powPattern.test(value)) {
								var elements = String(value.match(powPattern));
								var numbers = elements.split('^');
								
								value = value.replace(powPattern, Math.pow(numbers[0], numbers[1]));
							} else {
								break;
							}
						}
						
						// % replaced with percent
						var percentPattern = /\d+\*\d+\%/;
						while (strpos(value, '%', 0)) {
							if (percentPattern.test(value)) {
								var elements = String(value.match(percentPattern));
								var numbers = elements.split('*');
								numbers[1] = numbers[1].replace('%', '');
								
								value = value.replace(percentPattern, numbers[0] * (numbers[1] / 100));
							} else {
								break;
							}
						}
						
						percentPattern = /\d+\+\d+\%/;
						while (strpos(value, '%', 0)) {
							if (percentPattern.test(value)) {
								var elements = String(value.match(percentPattern));
								var numbers = elements.split('+');
								numbers[1] = numbers[1].replace('%', '');
								
								value = value.replace(percentPattern, parseFloat(numbers[0]) + (numbers[0] * (numbers[1] / 100)));
							} else {
								break;
							}
						}
						
						percentPattern = /\d+\-\d+\%/;
						while (strpos(value, '%', 0)) {
							if (percentPattern.test(value)) {
								var elements = String(value.match(percentPattern));
								var numbers = elements.split('-');
								numbers[1] = numbers[1].replace('%', '');
								
								value = value.replace(percentPattern, parseFloat(numbers[0]) - (numbers[0] * (numbers[1] / 100)));
							} else {
								break;
							}
						}
						
						percentPattern = /\d+\/\d+\%/;
						while (strpos(value, '%', 0)) {
							if (percentPattern.test(value)) {
								var elements = String(value.match(percentPattern));
								var numbers = elements.split('/');
								numbers[1] = numbers[1].replace('%', '');
								
								value = value.replace(percentPattern, numbers[0] / (numbers[1] / 100));
							} else {
								break;
							}
						}
						
						
						jQuery('#blackCalculator').val(eval(value));
					} catch (err) {
						alert("Procedimiento Incorrecto");
					}
					jQuery('#blackCalculator').focus();
				}
		
			}
			
			
	//-----------------------------------------------------------------------------------------------------------------
	
			 if (button == 'ret') {
				
					try {
						
				   var expresion1 = /^[0-9]+jQuery/; 
				   var expresion2 = /^[0-9]*\.?[0-9]*jQuery/; 
				   var expresion3 = /^[0-9]*\.jQuery/; 
				   
			if(value==""){
				jQuery('.isss').html(null);
				jQuery('.afp').html(null);
				jQuery('.isr_quincenal').html(null);
				jQuery('.isr_mensual').html(null);
                                jQuery('.total_retencion').html(null);
				jQuery('#tipo_sueldo').css("display","none");
				jQuery('#text_retencion').css("display","none");
				jQuery('#blackCalculator').focus();
				 }else{
				   
		if((expresion1.test(value))||(expresion2.test(value))){

			if(expresion3.test(value)){
				jQuery('.isss').html(null);
				jQuery('.afp').html(null);
				jQuery('.isr_quincenal').html(null);
				jQuery('.isr_mensual').html(null);
                                jQuery('.total_retencion').html(null);
				jQuery('#text_retencion').css("display","none");
				jQuery('#tipo_sueldo').css("display","none");
				alert("Expresión decimal incompleta");
				jQuery('#blackCalculator').focus();
			 }else{	
				jQuery('#text_retencion').css("display","block"); 				
               jQuery('#tipo_sueldo').css("display","block");
			   
                     //funcion change()
			jQuery('#tipo_sueldo').change(function() {

								seguro = (value * 0.03);
                           jQuery('.isss').html("ISSS: jQuery"+seguro.toFixed(2));
						   
						   	 aseguradora = (value * 0.0625);
                           jQuery('.afp').html("AFP: jQuery"+aseguradora.toFixed(2));
						   
						   
						     var renta1=0;
                                                     var renta2=0;
                                                     var renta=0;
						   jQuery('.isr_quincenal').html(null);	
					       jQuery('.isr_mensual').html(null);
		       //Salario mensual
					if(jQuery('#tipo_sueldo').val()=="m"){   
						   	if((value>0.01) && (value<=487.60)){
                                                            renta = (value);
							jQuery('.isr_mensual').html("Renta Mensual: jQuery"+renta.toFixed(2));
							}else if((value>487.60) && (value<=642.85)){
								renta1 = (value - 487.60);
                                                                renta2 = (renta1 * 0.1);
                                                                renta = (renta2 + 17.48);
								jQuery('.isr_mensual').html("Renta Mensual: jQuery"+renta.toFixed(2));
							  }else if((value>642.85) && (value<=915.81)){
								  renta1 = (value - 642.86);
                                                                  renta2 = (renta1  * 0.1);
                                                                  renta = (renta2 + 32.70);
								  jQuery('.isr_mensual').html("Renta Mensual: jQuery"+renta.toFixed(2));
								  }else if((value>915.81) && (value<=2058.67)){
									  renta1 = (value - 915.81);
                                                                          renta2 = (renta1 * 0.2);
                                                                          renta = (renta2 + 60.00);
									  jQuery('.isr_mensual').html("Renta Mensual: jQuery"+renta.toFixed(2));
									  }else if(value>2058.67){
										  renta1 = (value - 2058.67);
                                                                                  renta2 = (renta1 * 0.3);
                                                                                  renta = (renta2 + 288.57);
										  jQuery('.isr_mensual').html("Renta Mensual: jQuery"+renta.toFixed(2));
										  }
                                                           var total=seguro + aseguradora + renta;
                                                         jQuery('.total_retencion').html("Total Deducciones: jQuery"+total.toFixed(2));
                        //Salario quincenal
					}else if(jQuery('#tipo_sueldo').val()=="q"){	  
							if((value>0.01) && (value<=243.80)){
                                                            renta = (value);
							jQuery('.isr_quincenal').html("Renta Quincenal: jQuery"+(renta.toFixed(2)));
							}else if((value>243.80) && (value<=321.42)){
								renta1 = (value - 243.80);
                                                                renta2 = (renta1 * 0.1);
                                                                renta = (renta2 + 8.74);
								jQuery('.isr_quincenal').html("Renta Quincenal: jQuery"+renta.toFixed(2));
							  }else if((value>321.42) && (value<=457.90)){
								  renta1 = (value - 321.42);
                                                                  renta2 = (renta1 * 0.1);
                                                                  renta = (renta2 + 16.35);
								  jQuery('.isr_quincenal').html("Renta Quincenal: jQuery"+renta.toFixed(2));
								  }else if((value>457.90) && (value<=1029.33)){
									  renta1 = (value - 457.90);
                                                                          renta2 = (renta1 * 0.2);
                                                                          renta = (renta2 + 30.00);
									  jQuery('.isr_quincenal').html("Renta Quincenal: jQuery"+renta.toFixed(2));
									  }else if(value>1029.33){
										  renta1 = (value - 1029.33);
                                                                                  renta2 = (renta1  * 0.3);
                                                                                  renta = (renta2 + 144.28);
										  jQuery('.isr_quincenal').html("Renta Quincenal: jQuery"+renta.toFixed(2));
										  }
                                                           var total=seguro + aseguradora + renta;
                                                     jQuery('.total_retencion').html("Total Deducciones: jQuery"+total.toFixed(2));
                                                              
					 }else if(jQuery('#tipo_sueldo').val()=="n"){	
					 
				jQuery('.isss').html(null);
				jQuery('.afp').html(null);
				jQuery('.isr_quincenal').html(null);
				jQuery('.isr_mensual').html(null);
				jQuery('.total_retencion').html(null);
					 }
		         jQuery('#text_retencion').css("display","block");
                jQuery('#tipo_sueldo').css("display","block"); 
						  
				});
					 //fin de funcion change()
						  
			}			  
				     
					   
			}else{
				jQuery('.isss').html(null);
				jQuery('.afp').html(null);
				jQuery('.isr_quincenal').html(null);
				jQuery('.isr_mensual').html(null);
                                jQuery('.total_retencion').html(null);
				jQuery('#blackCalculator').focus();
				alert("Carácter innecesario");
				
				 }        
					}
					    } catch (err) {
						alert("rrrrrrr");
						
					    }
					
					
				}
	//---------------------------------------------------------------------------------------------------------------
	
	
			
		});
		
        return this;
    };
    
    jQuery.fn.blackCalculator.defaults = {
        type: 'simple',
        allowKeyboard: false,
		cssManual: false,
        language: {
            value: 'Value',
            backspace: 'Backspace',
            clear: 'Limpiar'
			
        }
		          
    };
    
          // css: 'css/',
          
    

    
	
	function inArray(haystack, needle)
    {
		var length = haystack.length;
		for(var i = 0; i < length; i++) {
			if(haystack[i] == needle) return true;
		}
		return false;
	};
	
	
	function strpos(haystack, needle, offset)
    {
		var i = (haystack + '').indexOf(needle, (offset || 0));
		return i === -1 ? false : i;
	};

})(jQuery);